//
// Created by Administrator on 24-9-29.
//

#include "mfmcustomplot.h"


void MFMCustomPlot::mousePressEvent(QMouseEvent* event)
{
    {
        if (event->button() == Qt::LeftButton)
        {
            int x_pos = event->pos().x();
            int y_pos = event->pos().y();
            double xv = this->xAxis->pixelToCoord(x_pos);
            double yv = this->yAxis->pixelToCoord(y_pos);
            int dis = INT_MAX;
            double res = yv;
            int idNo = -1;
            if (xv < xAxisValue && xv > 0)
            {
                for (int i = 0; i < this->graphCount(); i++)
                {
                    if(!graph(i)->visible())
                    {
                            continue;
                    }
                    if (abs(yv - graph(i)->data()->at(xv)->value) < dis)
                    {
                        dis = abs(yv - graph(i)->data()->at(xv)->value);
                        res = graph(i)->data()->at(xv)->value;
                        idNo = i;
                    }
                }
            }
            QString str;
            str = QString("\nx:%1\ny:%2").arg(QString::asprintf("%.3f", xv)).arg(QString::asprintf("%.3f", res));
            if (idNo != -1)
            {
                if (idNo == 8)
                {
                    str = str + QString("标准通道");
                }
                else
                {
                    str = str + QString("\nid:%1").arg(idNo+1);
                }
            }
            QToolTip::showText(cursor().pos(), str, this);
            tooltipVisible = true;
        }
    }
    QCustomPlot::mousePressEvent(event);
}

void MFMCustomPlot::mouseReleaseEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton && tooltipVisible)
    {
        hideTooltip();
        tooltipVisible = false;
    }
    QCustomPlot::mouseReleaseEvent(event);
}

MFMCustomPlot::MFMCustomPlot(QWidget* parent)
{
    setAttribute(Qt::WA_Hover);
}

MFMCustomPlot::~MFMCustomPlot()
{
}
